package com.eco.fanliapp.taobao;

import com.alibaba.fastjson.JSON;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkItemInfoGetRequest;
import com.taobao.api.request.TbkShopRecommendGetRequest;
import com.taobao.api.response.TbkItemInfoGetResponse;
import com.taobao.api.response.TbkShopRecommendGetResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Service
@Slf4j
public class ItemInfoController {
    public static void main(String[] args) {
        ItemInfoController itemInfoController = new ItemInfoController();

        TbkItemInfoGetResponse  tbkItemInfoGetResponse = itemInfoController.itemInfoByItemId("599995743280");
        log.info(JSON.toJSONString(tbkItemInfoGetResponse));

//        TbkShopRecommendGetResponse tbkShopRecommendGetResponse = itemInfoController.shopRecommend(430359524L);
//
    }

    /**
     * 根据商品id查询详情
    * @param itemId
     * @return
     */
    @RequestMapping(value = "/taobao/itemId/itemdetail", method = RequestMethod.GET)
    public TbkItemInfoGetResponse itemInfoByItemId(String itemId) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkItemInfoGetRequest req = new TbkItemInfoGetRequest();
        TbkItemInfoGetResponse rsp = null;
        req.setNumIids(itemId);
        try {
            rsp = client.execute(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
       return  rsp;
    }

//    /**
//     * 根据标题查询详情
//     * @param title
//     * @return
//     */
//    @RequestMapping(value = "/taobao/title/itemdetail", method = RequestMethod.POST)
//    public TbkItemGetResponse iteminfoByTitle(String title) {
//        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
//        TbkItemGetRequest req = new TbkItemGetRequest();
//        req.setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick");
//        req.setQ(title);
//        TbkItemGetResponse rsp = null;
//        try {
//            rsp = client.execute(req);
//            if (rsp.isSuccess() && rsp.getTotalResults() > 0) {
//                return rsp;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return rsp;
//    }

    /**
     * 根据淘宝卖家id查询店铺信息
     * @param userId
     * @return
     */
    @RequestMapping(value = "/taobao/shop/recommend", method = RequestMethod.POST)
    public TbkShopRecommendGetResponse shopRecommend(Long userId) {
        TaobaoClient client = new DefaultTaobaoClient(Config.TAO_URL, Config.TAO_APPKEY, Config.TAO_APPSECRET);
        TbkShopRecommendGetRequest req = new TbkShopRecommendGetRequest();
        req.setFields("user_id,shop_title,shop_type,seller_nick,pict_url,shop_url");
        req.setUserId(userId);
        req.setCount(20L);
        req.setPlatform(1L);
        TbkShopRecommendGetResponse rsp = null;
        try {
            rsp = client.execute(req);
            if (rsp.isSuccess() && rsp.getResults() != null && rsp.getResults().size() > 0) {
                return rsp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rsp;
    }
}
